﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace MS.InformationManagement.DAL.Support
{
    public static class CamlHelper
    {
        public static string GetInClausule(string filterField, List<string> selecetedValues)
        {
            string values = BuildIDFilters(selecetedValues);
            string inClausule = string.Format("<In><FieldRef Name='{0}'/><Values>{1}</Values></In>", "ContentType", values);
            return inClausule;
        }

        private static string BuildIDFilters(List<string> ids)
        {
            string filters = string.Empty;
            foreach (var i in ids)
            {
                filters += "<Value Type='Text'>" + i + "</Value>";
            }
            return filters;
        }

        public static SPListItemCollection GetChildItemByParentId(string lookUpInternalName, string childList, string parentId, SPWeb web)
        {
            SPQuery query = new SPQuery();
            query.Query = string.Format(@"<Where>
                                            <Eq>
                                                <FieldRef Name='{0}' LookupId='TRUE' />
                                                <Value Type='Lookup'>{1}</Value>
                                            </Eq>
                                          </Where>", lookUpInternalName, parentId);
            return web.Lists[childList].GetItems(query);
        }

        public static SPFile GetDocumentById(Guid listId, int itemId, SPWeb web)
        {
            SPQuery query = new SPQuery();
            return web.Lists[listId].GetItemById(itemId).File;
        }

        public static SPListItemCollection GetChildItemByParentIdByUser(string UserID, string childList, string InternalName)
        {
            SPQuery query = new SPQuery();
            StringBuilder camlQuery = new StringBuilder();
            camlQuery.Append("<Where><Eq><FieldRef Name='" + InternalName + "' LookupId='TRUE' /><Value Type='User'>");
            camlQuery.Append(UserID);
            camlQuery.Append("</Value></Eq></Where>");

            query.Query = camlQuery.ToString();

            return SPContext.Current.Web.Lists[childList].GetItems(query);
        }

        public static SPListItem GetParentItem(string parentList, int parentId, SPWeb web)
        {
            SPList targetList = web.Lists.TryGetList(parentList);
            if (targetList == null) { throw new Exception(string.Format("La lista {0} no existe en el sitio {1}", parentList, web.Url)); }
            SPListItem item = targetList.GetItemById(parentId);
            if (item == null) { throw new Exception(string.Format("No existe un item con id {0} en la lista {1}", parentId, parentList)); }
            return item;
        }

        public static SPListItemCollection GetRolesByUser(string List, int UserID, string InternalName)
        {
            SPQuery query = new SPQuery();
            StringBuilder camlQuery = new StringBuilder();
            camlQuery.Append("<Where><And><IsNull><FieldRef Name='Role_Page' /></IsNull>");
            camlQuery.Append("<Eq><FieldRef Name='");
            camlQuery.Append(InternalName);
            camlQuery.Append("' LookupId='TRUE' /><Value Type='User'>");
            camlQuery.Append(UserID);
            camlQuery.Append("</Value></Eq></And></Where>");


            query.Query = camlQuery.ToString();

            return SPContext.Current.Web.Lists[List].GetItems(query);
        }
    }
}
